from Common.BaseModel import db, BaseModel
from datetime import datetime
from Modules.sys_account.model import AccountModel


class LogModel(BaseModel):
    __tablename__ = 'sys_log'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    account_id = db.Column(db.Integer, db.ForeignKey('sys_account.id', ondelete='CASCADE', onupdate='CASCADE'))
    req_module = db.Column(db.String(64), nullable=False)
    req_method = db.Column(db.String(64), nullable=False)
    req_hostIp = db.Column(db.String(64), nullable=False)
    req_data = db.Column(db.Text)
    res_data = db.Column(db.Text)
    req_time = db.Column(db.DateTime)

    account = db.relationship('AccountModel', backref='logs')

    def __init__(self, account_id, dataDict):
        if not isinstance(dataDict, dict):
            return
        self.account_id = account_id
        self.req_module = dataDict['req_module']
        self.req_method = dataDict['req_method']
        self.req_hostIp = dataDict['req_hostIp']
        self.req_data = dataDict['req_data']
        self.res_data = dataDict['res_data']
        self.req_time = datetime.now()